package com.sdp.irrigation.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.sdp.core.bizc.vo.Result;
import com.sdp.irrigation.kafka.biz.CommandDownlinkBiz;
import com.sdp.irrigation.kafka.biz.impl.QueryBiz;
import com.sdp.irrigation.kafka.producer.KafkaProducer;
import com.sdp.irrigation.view.DownAgitationParam;
import com.sdp.irrigation.view.DownDesiltingParam;
import com.sdp.irrigation.view.DownEqCloseParam;
import com.sdp.irrigation.view.DownEqOpenParam;
import com.sdp.irrigation.view.DownFertigationParam;
import com.sdp.irrigation.view.DownSettleParam;
import com.sdp.irrigation.view.DownValveParam;
import com.sdp.irrigation.view.DownWaterSupplyParam;

@RestController
@RequestMapping("/CommandDownlinkController")
public class CommandDownlinkController {

	@Autowired
	private CommandDownlinkBiz commandDownlinkBiz;
	
	@Autowired
	private KafkaProducer kafkaProducer;
	
	@Autowired
	private QueryBiz queryBiz;
	
	/**
	 * 开关阀门
	 * @param downValveParam
	 * @return
	 */
	@PostMapping(value = "/downlinkCommandValve")
	public Result<?> downlinkCommandValve(@RequestBody DownValveParam downValveParam) {
		if (!StringUtils.isEmpty(downValveParam.getEqNo()) && !StringUtils.isEmpty(downValveParam.getIcNo())) {
			commandDownlinkBiz.downlinkCommandValve(downValveParam, kafkaProducer);
			return Result.ok("命令下发成功");
		}
		
		return Result.error("命令下发失败，所需参数不足");
	}
	
	/**
	 * 打开水泵
	 * @param downEqOpenParam
	 * @return
	 */
	@PostMapping(value = "/downlinkCommandEqOpen")
	public Result<?> downlinkCommandEqOpen(@RequestBody DownEqOpenParam downEqOpenParam) {
		Integer balance = null;
		if (!StringUtils.isEmpty(downEqOpenParam.getEqNo()) && !StringUtils.isEmpty(downEqOpenParam.getIcNo())) {
			balance = queryBiz.getIcBalance(downEqOpenParam.getIcNo());
			downEqOpenParam.setMoney(balance);
			commandDownlinkBiz.downlinkCommandEqOpen(downEqOpenParam, kafkaProducer);
			return Result.ok("命令下发成功");
		}
		return Result.error("命令下发失败，所需参数不足, balance = " + balance);
	}
	
	/**
	 * 关闭水泵
	 * @param downEqCloseParam
	 * @return
	 */
	@PostMapping(value = "/downlinkCommandEqClose")
	public Result<?> downlinkCommandEqClose(@RequestBody DownEqCloseParam downEqCloseParam) {
		if (!StringUtils.isEmpty(downEqCloseParam.getEqNo()) && !StringUtils.isEmpty(downEqCloseParam.getIcNo())) {
			commandDownlinkBiz.downlinkCommandEqClose(downEqCloseParam, kafkaProducer);
			return Result.ok("命令下发成功");
		}
		
		return Result.error("命令下发失败，所需参数不足");
	}
	
	/**
	 * 加水
	 * @param downWaterSupplyParam
	 * @return
	 */
	@PostMapping(value = "/downlinkCommandWaterSupply")
	public Result<?> downlinkCommandWaterSupply(@RequestBody DownWaterSupplyParam downWaterSupplyParam) {
		if (!StringUtils.isEmpty(downWaterSupplyParam.getEqNo()) && !StringUtils.isEmpty(downWaterSupplyParam.getIcNo())) {
			commandDownlinkBiz.downlinkCommandWaterSupply(downWaterSupplyParam, kafkaProducer);
			return Result.ok("命令下发成功");
		}

		return Result.error("命令下发失败，所需参数不足");
	}
	
	/**
	 * 搅拌
	 * @param downAgitationParam
	 * @return
	 */
	@PostMapping(value = "/downlinkCommandAgitation")
	public Result<?> downlinkCommandAgitation(@RequestBody DownAgitationParam downAgitationParam) {
		if (!StringUtils.isEmpty(downAgitationParam.getEqNo()) && !StringUtils.isEmpty(downAgitationParam.getIcNo())) {
			commandDownlinkBiz.downlinkCommandAgitation(downAgitationParam, kafkaProducer);
			return Result.ok("命令下发成功");
		}
		
		return Result.error("命令下发失败，所需参数不足");
	}
	
	/**
	 * 施肥
	 * @param downFertigationParam
	 * @return
	 */
	@PostMapping(value = "/downlinkCommandFertigation")
	public Result<?> downlinkCommandFertigation(@RequestBody DownFertigationParam downFertigationParam) {
		if (!StringUtils.isEmpty(downFertigationParam.getEqNo()) && !StringUtils.isEmpty(downFertigationParam.getIcNo())) {
			commandDownlinkBiz.downlinkCommandFertigation(downFertigationParam, kafkaProducer);
			return Result.ok("命令下发成功");
		}
		
		return Result.error("命令下发失败，所需参数不足");
	}
	
	/**
	 * 排沙
	 * @param downDesiltingParam
	 * @return
	 */
	@PostMapping(value = "/downlinkCommandDesilting")
	public Result<?> downlinkCommandDesilting(@RequestBody DownDesiltingParam downDesiltingParam) {
		if (!StringUtils.isEmpty(downDesiltingParam.getEqNo()) && !StringUtils.isEmpty(downDesiltingParam.getIcNo())) {
			commandDownlinkBiz.downlinkCommandDesilting(downDesiltingParam, kafkaProducer);
			return Result.ok("命令下发成功");
		}
			
		return Result.error("命令下发失败，所需参数不足");
	}
	
	/**
	 * 结账
	 * @param downSettleParam
	 * @return
	 */
	@PostMapping(value = "/downlinkCommandSettle")
	public Result<?> downlinkCommandSettle(@RequestBody DownSettleParam downSettleParam) {
		if (!StringUtils.isEmpty(downSettleParam.getEqNo()) && !StringUtils.isEmpty(downSettleParam.getIcNo())) {
			commandDownlinkBiz.downlinkCommandSettle(downSettleParam, kafkaProducer);
			return Result.ok("命令下发成功");
		}
		
		return Result.error("命令下发失败，所需参数不足");
	}
	
}
